﻿
namespace HSS.Xaml.UI.Interactivity
{
	#region Using Directives
	using System.Windows.Interactivity;
	using HSS.Xaml.UI.Controls;
	#endregion

	#region DoubleClickTrigger
	/// <summary>
	/// A base Trigger for handling a DoubleClick event.
	/// </summary>
	/// <typeparam name="T">The Type of the AssociatedObject.</typeparam>
	public abstract class DoubleClickTrigger<T> : TriggerBase<T>
		where T : System.Windows.UIElement
	{
		/// <summary>
		/// The DoubleClickDetection is used to handle the click event of the AssociatedObject.
		/// </summary>
		protected DoubleClickDetection DoubleClickDetection
		{
			get { return doubleClickDetection; }
			set { doubleClickDetection = value; }
		} private DoubleClickDetection doubleClickDetection;

		/// <summary>
		/// Called when the Trigger is being attached to the AssociatedObject.
		/// </summary>
		protected override void OnAttached()
		{
			base.OnAttached();
			if (AssociatedObject is System.Windows.UIElement)
			{
				DoubleClickDetection = new DoubleClickDetection();
				this.ConnectEvent();
			}
		}
		/// <summary>
		/// Connect to the AssociatedObject events that will trigger the DoubleClick.
		/// </summary>
		protected abstract void ConnectEvent();

		/// <summary>
		/// Called when the Trigger is being detached from the AssociatedObject.
		/// </summary>
		protected override void OnDetaching()
		{
			this.DisconnectEvent();
			base.OnDetaching();
		}
		/// <summary>
		/// Disconnect from the AssociatedObject events that will trigger the DoubleClick.
		/// </summary>
		protected abstract void DisconnectEvent();
		/// <summary>
		/// Call this method to process a double click.
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		protected void OnDoubleClick()
		{
			if (base.AssociatedObject != null)
			{
				foreach (var action in this.Actions)
				{
					var dca = action as IDoubleClickAction;
					if (null != dca && action.IsEnabled)
						dca.DoubleClickInvoke();
				}
			}
		}

	}
	#endregion
}
